{
 "cells": [
  {
   "cell_type": "code",
   "id": "0aca671b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:12:11.513113Z",
     "start_time": "2024-11-24T11:11:47.701359Z"
    }
   },
   "source": [
    "import torch"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "id": "b04dde50",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:12:11.960486Z",
     "start_time": "2024-11-24T11:12:11.577961Z"
    }
   },
   "source": [
    "x = torch.arange(12)\n",
    "x"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "id": "48cc610d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:12:17.328805Z",
     "start_time": "2024-11-24T11:12:17.321853Z"
    }
   },
   "source": [
    "x.shape"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([12])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "id": "57387f73",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:12:19.547406Z",
     "start_time": "2024-11-24T11:12:19.536433Z"
    }
   },
   "source": [
    "x.size()"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([12])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "id": "a68d5715",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:12:26.934338Z",
     "start_time": "2024-11-24T11:12:26.915361Z"
    }
   },
   "source": [
    "x.numel()"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:17:55.962585Z",
     "start_time": "2024-11-24T11:17:55.899726Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# X = x.reshape(3,4)\n",
    "#reshape维度可以用-1代替，自动计算\n",
    "X = x.reshape(3, -1)\n",
    "X"
   ],
   "id": "1192fc00",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:24:10.226777Z",
     "start_time": "2024-11-24T11:24:10.208824Z"
    }
   },
   "cell_type": "code",
   "source": "torch.zeros(2, 3, 4)",
   "id": "26fa9fda",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.]],\n",
       "\n",
       "        [[0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.]]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "id": "a5e2256d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:24:24.880319Z",
     "start_time": "2024-11-24T11:24:24.839428Z"
    }
   },
   "source": "torch.ones(2, 4, 3)",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[1., 1., 1.],\n",
       "         [1., 1., 1.],\n",
       "         [1., 1., 1.],\n",
       "         [1., 1., 1.]],\n",
       "\n",
       "        [[1., 1., 1.],\n",
       "         [1., 1., 1.],\n",
       "         [1., 1., 1.],\n",
       "         [1., 1., 1.]]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "id": "c012d3c2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:24:44.237032Z",
     "start_time": "2024-11-24T11:24:44.215090Z"
    }
   },
   "source": [
    "#随机化参数，生成均值为0，标准差为1的标准高斯分布的随机采样\n",
    "torch.randn(3, 4)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 1.1169, -1.5791, -0.4628, -1.8076],\n",
       "        [ 1.3898,  0.5443, -1.5175,  1.0079],\n",
       "        [-1.3495,  0.6714,  0.3167,  1.4493]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "cell_type": "code",
   "id": "a9de692e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:24:48.626454Z",
     "start_time": "2024-11-24T11:24:48.596535Z"
    }
   },
   "source": [
    "#确定张量初始化\n",
    "torch.tensor([[1, 2, 3], [4, 5, 6]])"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1, 2, 3],\n",
       "        [4, 5, 6]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "id": "91ce39e4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:25:01.673385Z",
     "start_time": "2024-11-24T11:25:01.619528Z"
    }
   },
   "source": [
    "# 运算符\n",
    "x = torch.tensor([1, 2, 4, 8])\n",
    "y = torch.tensor([2, 2, 2, 2])\n",
    "\n",
    "x + y, x - y, x * y, x / y, x ** y"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([ 3,  4,  6, 10]),\n",
       " tensor([-1,  0,  2,  6]),\n",
       " tensor([ 2,  4,  8, 16]),\n",
       " tensor([0.5000, 1.0000, 2.0000, 4.0000]),\n",
       " tensor([ 1,  4, 16, 64]))"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "id": "37b53cec",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:27:11.943361Z",
     "start_time": "2024-11-24T11:27:11.856564Z"
    }
   },
   "source": [
    "#指数运算\n",
    "x\n",
    "torch.exp(x)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([2.7183e+00, 7.3891e+00, 5.4598e+01, 2.9810e+03])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8647c486",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [ 2.,  1.,  4.,  3.],\n",
       "         [ 1.,  2.,  3.,  4.],\n",
       "         [ 5.,  6.,  7.,  8.]]),\n",
       " tensor([[ 0.,  1.,  2.,  3.,  2.,  1.,  4.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.,  1.,  2.,  3.,  4.],\n",
       "         [ 8.,  9., 10., 11.,  5.,  6.,  7.,  8.]]))"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#张量连接（从不同维度）\n",
    "x = torch.arange(12, dtype=torch.float32).reshape(3, 4)\n",
    "y = torch.tensor([[2, 1, 4, 3], [1, 2, 3, 4], [5, 6, 7, 8]])\n",
    "torch.cat((x, y), dim=0), torch.cat((x, y), dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "id": "4163347e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:28:37.011629Z",
     "start_time": "2024-11-24T11:28:36.965754Z"
    }
   },
   "source": [
    "x == y"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([False,  True, False, False])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "cell_type": "code",
   "id": "a78b54fa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:28:48.438383Z",
     "start_time": "2024-11-24T11:28:48.391437Z"
    }
   },
   "source": [
    "x.sum()"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(15)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "cell_type": "code",
   "id": "95a76185-3f5a-4a8c-8caa-acc19b8be125",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:29:02.307044Z",
     "start_time": "2024-11-24T11:29:02.296073Z"
    }
   },
   "source": [
    "a = torch.arange(3).reshape(3, 1)\n",
    "b = torch.arange(2).reshape(1, 2)\n",
    "a, b"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[0],\n",
       "         [1],\n",
       "         [2]]),\n",
       " tensor([[0, 1]]))"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "cell_type": "code",
   "id": "5047a9f1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:29:26.032901Z",
     "start_time": "2024-11-24T11:29:26.011864Z"
    }
   },
   "source": [
    "#广播机制，多数情况下，沿着长度为1 的轴进行广播（复制）\n",
    "a + b"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 1],\n",
       "        [1, 2],\n",
       "        [2, 3]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "cell_type": "code",
   "id": "31354b51",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:30:11.170696Z",
     "start_time": "2024-11-24T11:30:11.154738Z"
    }
   },
   "source": [
    "#索引和切片\n",
    "X"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 20
  },
  {
   "cell_type": "code",
   "id": "db298277",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:30:23.389077Z",
     "start_time": "2024-11-24T11:30:23.299586Z"
    }
   },
   "source": "X[-1], X[1:2]",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([ 8,  9, 10, 11]), tensor([[4, 5, 6, 7]]))"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "cell_type": "code",
   "id": "578c38f8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:30:54.670345Z",
     "start_time": "2024-11-24T11:30:54.615881Z"
    }
   },
   "source": [
    "X[1, 2] = 9\n",
    "X"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  9,  7],\n",
       "        [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "cell_type": "code",
   "id": "b5baf1a3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:31:03.833359Z",
     "start_time": "2024-11-24T11:31:03.796457Z"
    }
   },
   "source": [
    "X[0:2, :] = 12\n",
    "X"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[12, 12, 12, 12],\n",
       "        [12, 12, 12, 12],\n",
       "        [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "cell_type": "code",
   "id": "01aeb38b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:37:31.529834Z",
     "start_time": "2024-11-24T11:37:31.513876Z"
    }
   },
   "source": [
    "# 节省内存\n",
    "Y = X + 1\n",
    "before = id(Y)\n",
    "Y = Y + X\n",
    "after = id(Y)\n",
    "before == after\n",
    "# 为结果分配新的内存"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:42:12.860569Z",
     "start_time": "2024-11-24T11:42:12.847467Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用切片进行原地更新\n",
    "Z = torch.zeros_like(Y)\n",
    "print(id(Z))\n",
    "Z[:] = X + Y\n",
    "print(id(Z))"
   ],
   "id": "9d678bb4c7c25044",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2214971104128\n",
      "2214971104128\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T11:47:13.504878Z",
     "start_time": "2024-11-24T11:47:13.495899Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 转换numpy\n",
    "A = X.numpy()\n",
    "A[1][1] = 5\n",
    "B = torch.tensor(A)\n",
    "A,B\n",
    "# torch张量和numpy数组将共享它们的底层内存，就地操作更改一个张量也会同时更改另一个张量"
   ],
   "id": "7d069026ba0a5a51",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[12, 12, 12, 12],\n",
       "        [12,  5, 12, 12],\n",
       "        [ 8,  9, 10, 11]], dtype=int64),\n",
       " tensor([[12, 12, 12, 12],\n",
       "         [12,  5, 12, 12],\n",
       "         [ 8,  9, 10, 11]]))"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 33
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
